function [beta_J_hat, R2] = Jump_Beta(dFt, dPt, n, Delta_n, nobs, B_kl, L_k)
   

    M = size(dPt, 1);
    un = 3;
    K         =      size(L_k,1);
    H         =      sum(L_k);
    dFt_bar   =      zeros(H, n);
    Tn        =      n*Delta_n;
    vn        =      1000*log(1/(Tn*Delta_n));
    R2 = zeros(M,1);
    dPt_zero  =      dPt;
    dPt_zero(isnan(dPt)) = 0;

       
    % Jump regression
    % Use this matrix to filter factor jump to make sure we are considering
    % time period corresponding to each individual stock's live time. 
    dFt_filter_P = zeros(H, n, M);
    for i = 1:M
        dFt_filter_P(:,:,i) = repmat(~isnan(dPt(i,:)), H, 1);
    end
    
    % Step 1 Stack all the factor jump together into dFt_bar(H*n)
    dFt_cont_ix = fn_cont_ix(un, 0.47, dFt, Delta_n, n/nobs, nobs);
    dFt_jp_ix = ones(K,n) - dFt_cont_ix;
    
    id = zeros(H,n);
        
    for i = 1:K
        for j = 1:L_k(i)
            m = sum(L_k(1:i-1));
            id(m+j, :) = dFt_jp_ix(i,:) & dFt(i,:)>min(B_kl(m+j,:)) & dFt(i,:)<max(B_kl(m+j,:));
        end
    end
    
    for i = 1:K
        for j = 1:L_k(i)
            m = sum(L_k(1:i-1));
            dFt_bar(m+j,:) = dFt(i,:);
        end
    end
    
    dFt_bar = dFt_bar.*id;
    beta_J_hat = zeros(M,H);
    for i = 1:M
        if min(eig((dFt_bar.*dFt_filter_P(:,:,i))*(dFt_bar.*dFt_filter_P(:,:,i))')) > 1/vn
            beta_J_hat(i, :) = dPt_zero(i,:)*(dFt_bar') *...
                            inv((dFt_bar.*dFt_filter_P(:,:,i))*(dFt_bar.*dFt_filter_P(:,:,i))');
        else
            beta_J_hat(i, :) = 0;
        end

        R2_         = (beta_J_hat(i, :) * (dFt_bar.*dFt_filter_P(:,:,i))) * (beta_J_hat(i, :) * (dFt_bar.*dFt_filter_P(:,:,i)))';
        R2(i,1)         = R2_/((dPt_zero(i, :).*(sum(dFt_bar~=0)~=0))*(dPt_zero(i, :).*(sum(dFt_bar~=0)~=0))');
       
    end
    
